package cn.zcf.org.service.impl;

import cn.zcf.basic.exception.BusinessException;
import cn.zcf.basic.service.impl.BaseServiceImpl;
import cn.zcf.basic.utils.BaiduAuditUtils;
import cn.zcf.org.domain.Emp;
import cn.zcf.org.domain.Shop;
import cn.zcf.org.domain.ShopAuditLog;
import cn.zcf.org.dto.ShopDto;
import cn.zcf.org.mapper.EmpMapper;
import cn.zcf.org.mapper.ShopAuditLogMapper;
import cn.zcf.org.mapper.ShopMapper;
import cn.zcf.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.List;

@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    @Autowired
    private JavaMailSender javaMailSender;
    @Transactional
    @Override
    public void settlement(Shop shop) {
        if(StringUtils.isEmpty(shop.getName())){
            throw new BusinessException("店铺名称不能为空!");
        }
        //1.2.两次密码不一致校验 - 前端js校验了后端也要校验，因为前端js校验不安全可以跳过
        //如果这里要做：可以在Emp中加一个comfirmPassword字段，然后与密码判断是否相等
        //1.3.该店铺是否已经被入驻过 - 怎么判断入驻过？店铺名 + 地址
        Shop shopTemp = shopMapper.loadByNameAndAddress(shop);
        if(shopTemp != null){
            throw new BusinessException("店铺已经入驻，请直接登陆！忘记密码请找回密码");
        }
        if(!BaiduAuditUtils.TextCensor(shop.getName())){
            throw new BusinessException("店铺名称不合法，请重新输入！");
        }
        //二：入驻业务
        //2.1.保存店铺管理员信息t_employee
        Emp admin = shop.getAdmin();
        empMapper.add(admin);
        //2.2.保存店铺信息t_shop
        shop.setAdmin_id(admin.getId());
        shopMapper.add(shop);
        //2.3.将店铺的id更新到t_employee中
        admin.setShop_id(shop.getId());
        empMapper.update(admin);
    }
    @Transactional
    @Override
    public void pass(ShopAuditLog log) throws MessagingException  {
        //1.修改状态：state = 2
        Shop shop = shopMapper.loadById(log.getShopId());
        shop.setState(2);
        shopMapper.update(shop);

        //2.添加审核日志到数据库
        log.setState(2);
        log.setAuditId(325L);
        shopAuditLogMapper.add(log);

        //3.发送邮件：审核通过邮件 - 叫他激活
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"UTF-8");
        //设置发送方
        helper.setFrom("1493531772@qq.com");
        //设置主题
        helper.setSubject("店铺激活邮件");
        //设置内容
        helper.setText("<h1>你的店铺已经注册!!!</h1>" +
                "<img src='http://123.207.27.208"+shop.getLogo()+"' >" +
                "<a href='http://localhost:8080/shop/active/"+shop.getId()+"'>点击该链接激活</a>",true);
        //设置收件人
        Emp employee = empMapper.loadById(shop.getAdmin_id());
        //helper.setTo(employee.getEmail());
        helper.setTo("1493531772@qq.com");
        //发送
        javaMailSender.send(mimeMessage);
    }
    @Transactional
    @Override
    public void reject(ShopAuditLog log) throws MessagingException  {
        // 1. 修改shop状态
        // 通过shop_id 查询shop数据  ，再修改状态
        Shop shop = shopMapper.loadById(log.getShopId());
        // 审核通过
        shop.setState(4);
        shopMapper.update(shop);

        // 2. 录入日志  shopLog
        log.setState(4);
        log.setAuditId(1L);
        shopAuditLogMapper.add(log);

        // 3. 通知商家   邮件发送
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("1493531772@qq.com");
        helper.setSubject("驳回邮件");
        helper.setText("<h1>你的店铺入驻失败!!!</h1><a href='http://localhost:8080/shop/active/"+shop.getId()+"'>点击该链接激活</a>",true);

        // 获取收件人
        Emp employee = empMapper.loadById(shop.getAdmin_id());
        String email = employee.getEmail();
        helper.setTo("1493531772@qq.com");
        // 发送
        javaMailSender.send(mimeMessage);

        /**
         * 两种情况:
         *    1. 修改状态为驳回  state = 4; 下次入驻的时候判断该店铺是否是被驳回过，如果是就直接继续入驻。
         *    2. 直接删除 shop,  emp
         *
         */
    }

    @Override
    public List<ShopDto> getEcharts() {
        return shopMapper.getEcharts();
    }
}
